trigger OnSPUpdate on Support_Program__c (before insert, before update, before delete) {
    if ((Trigger.isInsert)||(Trigger.isUpdate)) {
        for (Support_Program__c sp : trigger.new) {
            try 
            {
                //
                // Enforce locked field
                //
                if (!SP_Trigger.DefEntHintUnlocked) 
                {
                    if ((trigger.isUpdate) && (trigger.oldMap.get(sp.Id).DefEntHint__c != trigger.newMap.get(sp.Id).DefEntHint__c))
                    {
                        //sp.DefEntHint__c.addError('Read only field');
                        trigger.newMap.get(sp.Id).DefEntHint__c = trigger.oldMap.get(sp.Id).DefEntHint__c;
                    } 
                    else if ((trigger.isInsert) && (sp.DefEntHint__c != null)) 
                    {
                        //sp.DefEntHint__c.addError('Read only field');
                        sp.DefEntHint__c = null;
                    }
                }
                
                // For MASP (which is the default), force SP.Account to null and SP.ApplyToAll to true
                // For NCSP, force SP.ApplyToAll to false.
                if ((sp.RecordTypeId==null)||(sp.RecordTypeId==RT.MASPId))  {
                    sp.Account__c=null; 
                    sp.Apply_To_All_Contacts_In_Account__c=True; 
                    SP_Trigger.ValidateSP(sp,SP_Trigger.SPType.MASP);
                } else if (sp.RecordTypeId==RT.NCSPId) { 
                    //sp.Apply_To_All_Contacts_In_Account__c=False;
                    SP_Trigger.ValidateSP(sp,SP_Trigger.SPType.NCSP); 
                } else {
                    // No validation -- SP is probably a legacy type like Template
                }
            } catch(Exception e) {
                sp.addError( 'An unexpected error occurred during validation:  ' + e.getMessage() );
            }
        }
    }
    
}